package com.pf.controller;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.pf.dto.ClientDTO;
import com.pf.dto.OrderDTO;
import com.pf.utils.JacksonUtils;
import com.pf.utils.PwdEncryptUtils;
import com.pf.utils.RestClient;
import com.pf.utils.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2021/9/8
 */
@RestController
public class TestController {


    @Autowired
    private RestClient restClient;

    /**
     * 客户端唯一标识：该值一般由服务提供方提供固定值
     */
    private static final String APP_ID = "client_1001";
    /**
     * 客户端唯一密码：该值一般由服务提供方提供固定值
     */
    private static final String APP_SECRET = "abc123";

    /**
     * 客户端密码盐值：该值一般由服务提供方提供固定值
     */
    private static final String APP_SECRET_SALT = "123456";


    /**
     * 测试创建token接口
     * <p>
     * 测试url：http://localhost:8080/test/token/create
     *
     * @return
     */
    @GetMapping("/test/token/create")
    public String testCreateToken() {
        String url = "http://localhost:8081/token/create";
        String decryptAppSerect = PwdEncryptUtils.md5AndSha(APP_SECRET + APP_SECRET_SALT);
        ClientDTO clientDTO = ClientDTO.builder().appId(APP_ID).decryptAppSecret(decryptAppSerect).build();
        Map<String, String> reqHeaders = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        String result = restClient.executePostJson(url, null, clientDTO, reqHeaders, String.class);
        return result;
    }


    /**
     * 测试下订单接口
     * <p>
     * 测试url：http://localhost:8080/test/order?token=xxxxxxx
     *
     * @param token
     * @return
     */
    @GetMapping("/test/order")
    public String testOrder(@RequestParam("token") String token) throws Exception {
        String url = "http://localhost:8081/order";
        // 1.组装请求参数
        String requestId = UUID.randomUUID().toString();
        String timestamp = System.currentTimeMillis() + "";
        Map<String, String> reqParams = new HashMap<>();
        reqParams.put("appId", APP_ID);
        reqParams.put("requestId", requestId);
        reqParams.put("timestamp", timestamp);
        String reqBodyStr = getOrderRequestData();
        // 生成签名
        String sign = SignUtils.sign(APP_SECRET, reqParams, reqBodyStr);
        reqParams.put("sign", sign);

        // 2.组装请求头
        Map<String, String> reqHeaders = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE, "token", token);
        // 3.远程调用下单接口
        String result = restClient.executePostJson(url, reqParams, reqBodyStr, reqHeaders, String.class);
        return result;
    }


    /**
     * 获取模拟下单的业务请求参数
     *
     * @return
     */
    private static String getOrderRequestData() {
        // 创建父子订单信息
        OrderDTO subOrder1 = OrderDTO.builder().orderId("1001").orderName("子订单1").type(2).totalPrice(new BigDecimal("10")).build();
        OrderDTO subOrder2 = OrderDTO.builder().orderId("1002").orderName("子订单2").type(2).totalPrice(new BigDecimal("20")).build();
        List<OrderDTO> subOrders = Lists.newArrayList(subOrder1, subOrder2);
        OrderDTO parentOrder = OrderDTO.builder().orderId("1").orderName("父订单").type(1).totalPrice(new BigDecimal("30")).build();
        parentOrder.setSubOrders(subOrders);
        return JacksonUtils.toJsonString(parentOrder);
    }

}
